package com.example.android.ui.mine

import android.annotation.SuppressLint
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.fragment.app.Fragment
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import com.blankj.utilcode.util.ToastUtils
import com.example.android.databinding.FragmentMineBinding
import com.example.android.ui.activity.AppbarActivity
import com.zxf.basic.expand.launch
import com.zxf.basic.expand.signClick
import com.zxf.basic.expand.startActivityResult
import io.reactivex.BackpressureStrategy
import io.reactivex.Flowable
import io.reactivex.FlowableOnSubscribe
import io.reactivex.FlowableSubscriber
import io.reactivex.Observable
import io.reactivex.ObservableOnSubscribe
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.Disposable
import io.reactivex.schedulers.Schedulers
import org.reactivestreams.Subscription

class MineFragment : Fragment() {

    private lateinit var notificationsViewModel: MineViewModel
    private lateinit var binding: FragmentMineBinding

    private val launcher = startActivityResult {
        ToastUtils.showShort(it.data?.getStringExtra("name"))
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        notificationsViewModel = ViewModelProvider(this).get(MineViewModel::class.java)

        binding = FragmentMineBinding.inflate(inflater, container, false)
        val root: View = binding.root

        val textView: TextView = binding.textNotifications
        notificationsViewModel.text.observe(viewLifecycleOwner, Observer {
            textView.text = it
        })
        return root
    }

    @SuppressLint("CheckResult")
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        binding.btnAppbar.signClick {
            launcher.launch<AppbarActivity>(requireContext()) {
                putExtra("name", "jack")
            }
        }
        binding.btnObservable.setOnClickListener {
            Observable.create(ObservableOnSubscribe<Int> {
                for (i in 1 .. 10) {
                    it.onNext(i)
                }
                it.onComplete()
            }).subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(object: io.reactivex.Observer<Int> {
                    override fun onSubscribe(d: Disposable) {
                        ToastUtils.showShort("开始订阅")
                    }

                    override fun onNext(t: Int) {
                        println("接收数据-》$t")
                    }

                    override fun onError(e: Throwable) {

                    }

                    override fun onComplete() {
                        ToastUtils.showShort("结束订阅")
                    }

                })
        }
        binding.btnFlowable.setOnClickListener {
            Flowable.create(FlowableOnSubscribe<Int> {
                for (i in 1 .. 10000) {
                    it.onNext(i)
                }
                it.onComplete()
            }, BackpressureStrategy.DROP)
                .subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(object: FlowableSubscriber<Int> {
                    override fun onSubscribe(s: Subscription) {
                        ToastUtils.showShort("开始订阅")
                        s.request(10000)
                    }

                    override fun onNext(t: Int?) {
                        println("接收数据-》$t")
                    }

                    override fun onError(t: Throwable?) {

                    }

                    override fun onComplete() {
                        ToastUtils.showShort("结束订阅")
                    }

                })
        }
        binding.btnMap.setOnClickListener {
            Observable.just(1, 2, 3, 4, 5)
                .map { t -> "item-$t" }
                .subscribe { t -> println("接收的数据：$t") }
        }
        binding.btnFlatMap.setOnClickListener {
            Observable.just(1, 2, 3, 4, 5)
                .flatMap { t ->
                    return@flatMap Observable.just("flatmap->$t")
                }
                .subscribe { t -> println("接收的数据：$t") }
        }
        binding.btnConcatMap.setOnClickListener {
            Observable.just(1, 2, 3, 4, 5)
                .concatMap { t -> Observable.just("map->$t") }
        }
    }

}